﻿var path = this.path;
define([
    'dojo/_base/declare',
    'esri/SpatialReference',
    'esri/tasks/ProjectParameters',
    'geomap/plugins/layers/ExGraphicsLayer',
], function (declare, SpatialReference, ProjectParameters, GraphicsLayer) {
    var PointLayer = declare([GraphicsLayer], {
        setData: function (mapView, layer) {
            var me = this;

            me.clear();
            if (!mapView) {
                return;
            }
            me.mapView = mapView;
            var mapUtils = mapView.mapUtils;
            me._setLayer(layer);
            layer.layer.onClick = me.onClick.bind(me);
            var options = layer.layer || {};
            me.options = options;
            var data = $.extend(true, [], options.data);

            if (options.isClickReorderGraphic != undefined) {
                options.isClickReorderGraphic = options.isClickReorderGraphic;
            } else {
                options.isClickReorderGraphic = true;
            }
            if (options.isClickReorderLayer != undefined) {
                options.isClickReorderLayer = options.isClickReorderLayer;
            } else {
                options.isClickReorderLayer = true;
            }

            me.popupOptions = Object.assign(
                {
                    visible: false,
                    isMultiPopup: false,
                    contentField: 'content',
                    isShowCloseIcon: false,
                },
                options.popupOptions
            );

            me.highlightOptions = Object.assign(
                {
                    sizeFactor: 1.2,
                    widthFactor: 1.2,
                    heightFactor: 1.2,
                    visible: false,
                },
                options.highlightOptions
            );

            me.__currentGraphics = [];
            if (me.__mapOnLoadHandle) {
                clearInterval(me.__mapOnLoadHandle);
            }
            var defaultSymbol = {
                type: 'imagePoint',
                url: path + './../../one-map/1.0.0/src/themes/images/panoramal.png',
                height: 31,
                width: 22,
            };
            me.__mapOnLoadHandle = setInterval(function () {
                if (mapView._isMapOnLoad) {
                    clearInterval(me.__mapOnLoadHandle);

                    if (!data || data.length < 1) {
                        if (typeof options.callback == 'function') {
                            options.callback(layer);
                        }
                    } else {
                        var lngLatArray = mapUtils.toLngLatArray(mapView, data, options);
                        var symbolFieldName =
                            options.symbolFieldName || mapView.getSymbolFieldName();
                        var spatialReference = me.spatialReference;

                        if (options.outSr) {
                            var PrjParams = new ProjectParameters();
                            PrjParams.outSR = new SpatialReference({ wkid: options.outSr });
                            PrjParams.geometries = [];
                            lngLatArray.map(function (lngLat, index) {
                                var point = mapUtils.createPoint(mapView, lngLat, {
                                    sr: spatialReference,
                                });
                                PrjParams.geometries.push(point);
                            });
                            mapView.geometryService.project(PrjParams, function (outputPoints) {
                                console.log(outputPoints);
                                lngLatArray.map(function (lngLat, index) {
                                    debugger;
                                    var symbol =
                                        lngLat[symbolFieldName] || options.symbol || defaultSymbol;
                                    mapUtils.getSymbol(mapView, symbol, function (symbol) {
                                        var attributes = lngLat;
                                        if (lngLat.x > 0 && lngLat.y > 0) {
                                            var graphic = mapUtils.createGraphic(mapView, {
                                                geometry: outputPoints[index],
                                                symbol: symbol,
                                                attributes: attributes,
                                            });
                                            me.add(graphic);
                                        }
                                        if (index == lngLatArray.length - 1) {
                                            me._calCurrentGraphics();
                                            if (options.labelOptions) {
                                                me.labelOptions = mapUtils.$.extend(
                                                    true,
                                                    {
                                                        textField: 'name',
                                                        ptThreshold: 0, //当前窗口的点位少于这个最大值时，标签强制展示
                                                        showLabels: 1, //1:根据比例尺展示，2：强制展示，0：强制不展示
                                                        scaleInfo: {
                                                            maxScale: 0,
                                                            minScale: 0,
                                                        },
                                                        labelSymbol: {
                                                            labelClass: 'labelInfo',
                                                            labelStyle: {
                                                                offsety: 18,
                                                                offsetx: 0,
                                                                position: 'top',
                                                            },
                                                        },
                                                    },
                                                    options.labelOptions
                                                );

                                                me._showLabels(mapView, options);
                                            }
                                            if (typeof options.callback == 'function') {
                                                options.callback(layer);
                                            }
                                        }
                                    });
                                });
                            });
                        } else {
                            lngLatArray.map(function (lngLat, index) {
                                var symbol =
                                    lngLat[symbolFieldName] || options.symbol || defaultSymbol;
                                mapUtils.getSymbol(mapView, symbol, function (symbol) {
                                    var attributes = lngLat;
                                    if (lngLat.x > 0 && lngLat.y > 0) {
                                        var point = mapUtils.createPoint(mapView, lngLat, {
                                            sr: spatialReference,
                                        });

                                        var graphic = mapUtils.createGraphic(mapView, {
                                            geometry: point,
                                            symbol: symbol,
                                            attributes: attributes,
                                        });
                                        me.add(graphic);
                                    }
                                    if (index == lngLatArray.length - 1) {
                                        me._calCurrentGraphics();
                                        if (options.labelOptions) {
                                            me.labelOptions = mapUtils.$.extend(
                                                true,
                                                {
                                                    textField: 'name',
                                                    ptThreshold: 0, //当前窗口的点位少于这个最大值时，标签强制展示
                                                    showLabels: 1, //1:根据比例尺展示，2：强制展示，0：强制不展示
                                                    scaleInfo: {
                                                        maxScale: 0, // 1000000,
                                                        minScale: 0,
                                                    },
                                                    labelSymbol: {
                                                        labelClass: 'labelInfo',
                                                        labelStyle: {
                                                            offsety: 18,
                                                            offsetx: 0,
                                                            position: 'top',
                                                        },
                                                    },
                                                },
                                                options.labelOptions
                                            );

                                            me._showLabels(mapView, options);
                                        }
                                        if (typeof options.callback == 'function') {
                                            options.callback(layer);
                                        }
                                    }
                                });
                            });
                        }

                        if (options.popupOptions && options.popupOptions.visible) {
                            me.openCustomPopup(mapView, lngLatArray);
                        }
                        // if (options.highlightOptions && options.highlightOptions.visible) {
                        me._showHighlight(mapView, options);
                        // }
                        // ;
                    }
                }
            });
        },
        getGraphicLocation: function (mapView, esriEvent) {
            if (!esriEvent || !esriEvent.graphic) {
                return;
            }
            if (!mapView) {
                return;
            }
            var map = mapView.getMap();
            if (!map) {
                return;
            }
            var geometry = esriEvent.graphic.geometry || {};
            var lngFieldName = mapView.getLngFieldName();
            var latFieldName = mapView.getLatFieldName();
            var location = {};
            location[lngFieldName] = geometry.x;
            location[latFieldName] = geometry.y;
            location.sr = geometry.spatialReference;

            return location;
        },
        onClick(e) {
            var me = this;
            var mapView = me.mapView;
            mapView.mapUtils._currentClickLayer = me.layerName;
            var data = e.graphic.attributes;
            var center = { x: data.x, y: data.y };

            mapView.zoomToCenter(center, {
                callback: function () {
                    mapView._require.async(
                        path + './../../one-map/1.0.0/src/mode/components/psvComView',
                        function (PsvComView) {
                            var psvComView = new PsvComView({
                                visible: false,
                            });
                            psvComView.setBaseMapLayer(data.baseMapLayer);
                            psvComView.setCenter(center);
                            psvComView.setZoomLevel(data.psvLevel);
                            psvComView.setSphereCorrection({
                                pan: data.pan || 0,
                                tilt: data.tilt || 0,
                                roll: data.roll || 0,
                            });
                            psvComView.appendTo(mapView.getElement());
                            setTimeout(function () {
                                psvComView.show();
                            }, data.timeout || 2000);
                        }
                    );
                },
            });
        },
    });
    return PointLayer;
});
